$(function () {
    axios.defaults.baseURL = 'http://www.liulongbin.top:3007'
    var addindex = 0
    initArtCateList()
    function initArtCateList() {
        axios({
            mthod: 'get',
            url: '/my/article/cates',
        }).then(res => {
            // console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            let arr = []
            res.data.data.forEach(function (ele) {
                arr.push(`
                <tr>
                    <td>${ele.Id}</td>
                    <td>${ele.name}</td>
                    <td>${ele.alias}</td>
                    <td>
                        <button class="layui-btn layui-btn-xs">修改</button>
                        <button class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
                    </td>
                </tr>
                `)
            })
            $('tbody').html(arr.join(''))
        })
    }
    $('#addbtn').on('click', function () {
         addindex=  layer.open({
            type: 1,
            title: '添加文章分类',
            area: ['500px', '300px'],
            content: `
              <form id="addForm" class="layui-form" action="" style="padding:20px 30px 0 0;">
            <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类别名</label>
            <div class="layui-input-block">
                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
              `
        });
    })
    $('body').on('submit','#addForm',function(e){
        e.preventDefault()
        axios({
            method:'post',
            url:'/my/article/addcates',
            data:$(this).serialize()
        }).then(({data:res})=>{
            if(res.status !==0){
                return layui.layer.msg(res.message);
            }
            layui.layer.msg(res.message);
            initArtCateList()
            layer.close(addindex)
        })
    })
    
})